﻿Imports log4net
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports System
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Class AddFundCenterToProject
    Inherits System.Web.UI.Page

    Public ScriptTxt As String = ""
    Public dmax As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("AddFundCenterToProject")
   
    Sub Alert(ByVal texter As String)
        ScriptTxt = "<script>alert('" & texter & "')</script>"
    End Sub

    Protected Sub CancelBtn_Click(sender As Object, e As ImageClickEventArgs) Handles CancelBtn.Click
        ScriptTxt = "<script>window.parent.TINY.box.hide();</script>"
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If

        WebGrid123.LayoutSettings.AllowAddNew = ISNet.WebUI.WebGrid.AddNew.Yes


    End Sub

    Protected Sub WebGrid123_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid123.InitializeDataSource
        Dim adapter As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter

        Dim dtTable As New B10DataSet.ProjectFundCentersDataTable


        Session.Add("AddFundCenterToProjectCode", Request("projectCode"))

        Dim projectCode As String = Request("projectCode")
        Dim planYear As String = Request("planYear")

        Dim sql As String = "select * from ProjectFundCenters "
        sql += " WHERE 1 = 1 "

        sql += " AND ProjectCode = '" & projectCode & "'"
        sql += " AND PlanYear = " & planYear
        sql += " ORDER BY FromFundCenter_FundCenterCode "

        adapter.GetDataByFilter(dtTable, sql)
        'Response.Write(sql)
        Session("count_data") = dtTable.Rows.Count
        dtTable.Columns("ToFundCenter_PlanYear").AllowDBNull = True
        dtTable.Columns("ToFundCenter_FundCenterCode").AllowDBNull = True
        dtTable.Columns("FromFundCenter_PlanYear").AllowDBNull = True
        dtTable.Columns("FromFundCenter_FundCenterCode").AllowDBNull = True
        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("ProjectCode").AllowDBNull = True
        dtTable.Columns("Id").ReadOnly = False


        e.DataSource = dtTable

    End Sub


    Protected Sub WebGrid123_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)

        Dim adapter As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter
        Dim projectCode As String = TryCast(Session("AddFundCenterToProjectCode"), String)

        Dim check = adapter.CheckExistFundCenter(Session("PlanYear"), projectCode, e.Row.Cells(2).Value, e.Row.Cells(3).Value)
        Dim id As Decimal
        If check <> 0 Then
            WebGrid123.ClearCachedDataSource()
            Throw New ArgumentException("ข้อมูลโครงการมี Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
            WebGrid123.ClientAction.Refresh()
        Else
            If Val(e.Row.Cells(3).Value) < Val(e.Row.Cells(2).Value) Then
                WebGrid123.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Fund Center น้อยกว่า จาก กรุณาตรวจสอบ")
            End If

            id = adapter.InsertQuery(Session("PlanYear"), Date.Now, Session("users"), projectCode, e.Row.Cells(2).Value, Session("PlanYear"), e.Row.Cells(3).Value, Session("PlanYear"))

        End If

        Dim dr As DataRow = e.Row.DataRow
        dr("Id") = id
    End Sub

    Protected Sub WebGrid123_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim projectCode As String = TryCast(Session("AddFundCenterToProjectCode"), String)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter

        Dim check = adapter.CheckExistUpdate(Session("PlanYear"), projectCode, id, e.Row.Cells(2).Value, e.Row.Cells(3).Value)
        If check <> 0 Then
            WebGrid123.ClearCachedDataSource()
            WebGrid123.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลผู้ใช้งานมี Fund Center ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else

            adapter.UpdateQuery(Date.Now, Session("users"), e.Row.Cells(2).Value, e.Row.Cells(3).Value, id)

        End If
    End Sub

    Protected Sub WebGrid123_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
        e.Row.Cells(1).Text = e.Row.DataSourceIndex + 1
    End Sub

    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.FundCenters2TableAdapter
        Dim dt As New MasterDataSet.FundCenters2DataTable
        dt = adapter.GetDataByPlanYear(Val(Request("planYear")))

        e.DataSource = dt
    End Sub
    Protected Sub FundCenterCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo2.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.FundCenters2TableAdapter
        Dim dt As New MasterDataSet.FundCenters2DataTable
        dt = adapter.GetDataByPlanYear(Val(Request("planYear")))

        e.DataSource = dt
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid123.ClearCachedDataSource()
        WebGrid123.RebindDataSource()
    End Sub



    Protected Sub WebGrid123_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Delete As String = TryCast(Request("id"), String)
            Dim adapter As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter
            If type = "1" Then
                ' MsgBox(Deletet)
                adapter.DeleteQuery(Delete)
            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid123.RootTable.GetCheckedRows()
                    adapter.DeleteQuery(keyValue)
                Next keyValue
            End If

            WebGrid123.ClientAction.Refresh()
        End If

    End Sub
End Class
